﻿using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Dispatcher;
using Lifter.Device.Unit;
using Lifter.Global;
using Lifter.Gui.Monitor.Panels;
using Lifter.Utility;
using Sunny.UI;
using System;
using System.Collections.Generic;

namespace Lifter.Gui.Monitor.Popups
{
    public partial class CommandEdit : UIEditForm
    {
        private LifterSEM lftSem = null;
        public CommandEdit()
        {
            InitializeComponent();
            if (this.lftSem == null)
            {
                this.lftSem = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            }
            this.LoadcbCommandIDData();
            this.LoadcbMaterialIDData();
            this.LoadcbFromData();
            this.LoadcbDestData();
            this.LoadcbCommandStatusData();
        }

        protected override bool CheckData()
        {
            return CheckEmpty(cbCommandID, "请输入指令ID")
                   && CheckEmpty(cbMaterialID, "请输入物料ID")
                   && CheckEmpty(cbFrom,"请输入起始地")
                   && CheckEmpty(cbDest, "请输入目的地")
                   && CheckEmpty(cbCommandStatus, "请输入指令状态");
        }
        /// <summary>
        /// 更新指令信息
        /// </summary>
        public void UpdateCommandInfo()
        {
            string commandID = this.cbCommandID.Text;
            //获取指令信息
            TransferWorkItem transferWorkItem = this.lftSem.LDispatcher.GetJob(commandID);
            if (transferWorkItem != null)
            {
                transferWorkItem.CarrierID = this.cbMaterialID.Text;
                transferWorkItem.Source = this.cbFrom.Text;
                transferWorkItem.Dest = this.cbDest.Text;
                try
                {
                    //将字符串string类型转成枚举类型
                    transferWorkItem.TransferState = (enumTransferState)Enum.Parse(typeof(enumTransferState), this.cbCommandStatus.Text);
                }
                catch (Exception ex)
                {
                    LogHelper.loggerError("在手动更新指令信息时，转换指令状态时报错",ex);
                }

                /*                if (enumTransferState.LOADREQ.ToString().Equals(this.cbCommandStatus.Text))
                                {
                                    transferWorkItem.TransferState = enumTransferState.LOADREQ;
                                }
                                else if (enumTransferState.ONSHUTTLE.ToString().Equals(this.cbCommandStatus.Text))
                                {
                                    transferWorkItem.TransferState = enumTransferState.ONSHUTTLE;
                                }*/
                int result = transferWorkItem.Update();
                //如果指令状态更改为ONSHUTTLE状态,更新指令位置信息，以及指令状态信息
                if (transferWorkItem.TransferState == enumTransferState.ONSHUTTLE)
                {
                    //更新指令的状态
                    Vehicle vehicle = this.lftSem.GetVehicle(transferWorkItem.CarrierLoc);
                    if (vehicle != null)
                    {
                        
                        transferWorkItem.IsOnShuttle = true;
                        vehicle.CommandId = transferWorkItem.CommandID;
                    }
                    else
                    {
                        this.ShowWarningDialog($"修改指令状态为{transferWorkItem.TransferState}时，指令{transferWorkItem.CommandID}当前位置{transferWorkItem.CarrierLoc}不正确");
                        LogHelper.loggerInfo($"修改指令状态为{transferWorkItem.TransferState}时，指令{transferWorkItem.CommandID}当前位置{transferWorkItem.CarrierLoc}不正确");
                    }
                }
                if (result > 0)
                {
                    this.ShowSuccessDialog("指令信息修改成功");
                    MainTransferQueuePanel mainTransferQueuePanel = (MainTransferQueuePanel)SimpleCache.GetObject("OperationForm", "MainTransferQueuePanel");
                    mainTransferQueuePanel.UpdateDelegate();
                    LogHelper.operationLogger(string.Format("用户[{0}]修改指令成功，指令ID为:[{1}]", this.lftSem.UserName, commandID));
                    lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]修改指令成功，指令ID为:[{1}]", this.lftSem.UserName, commandID));
                }
                else
                {
                    this.ShowWarningDialog("指令信息修改失败");
                    LogHelper.operationLogger(string.Format("用户[{0}]修改指令失败，指令ID为:[{1}]", this.lftSem.UserName, commandID));
                    lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]修改指令失败，指令ID为:[{1}]", this.lftSem.UserName, commandID));
                }
            }
            else
            {
                this.ShowInfoDialog("在更新指令界面，更新指令时，指令信息不存在");
                LogHelper.loggerInfo("在更新指令界面，更新指令时，指令信息不存在");
                LogHelper.operationLogger(string.Format("用户[{0}]修改指令失败，指令ID为:[{1}]，失败原因:[指令信息不存在]", this.lftSem.UserName, commandID));
                lftSem.InsertUserOperation(lftSem.UserName, string.Format("用户[{0}]修改指令失败，指令ID为:[{1}]，失败原因:[指令信息不存在]", this.lftSem.UserName, commandID));
            }
        }
        /// <summary>
        /// 加载指令控件数据
        /// </summary>
        private void LoadcbCommandIDData()
        {
            List<TransferWorkItem> transferWorkItems = this.lftSem.LDispatcher.workList;
            List<string> commandIDs = new List<string>();
            foreach (TransferWorkItem transferWork in transferWorkItems)
            {
                commandIDs.Add(transferWork.CommandID);
            }
            this.cbCommandID.Items.AddRange(commandIDs.ToArray());
        }
        /// <summary>
        /// 加载  物料ID控件数据
        /// </summary>
        private void LoadcbMaterialIDData()
        {
            this.cbMaterialID.Items.Clear();
            List<string> carriers = Naming.GetCarrierManager().GetCarriersId();
            this.cbMaterialID.Items.AddRange(carriers.ToArray());
        }
        /// <summary>
        /// 加载起始地控件数据
        /// </summary>
        private void LoadcbFromData()
        {
            List<string> fromID = new List<string>();
            fromID = this.lftSem.LDispatcher.GetCommandSources();
            this.cbFrom.Items.AddRange(fromID.ToArray());
        }
        /// <summary>
        /// 加载目的地控件数据
        /// </summary>
        private void LoadcbDestData()
        {
            List<string> destID = new List<string>();
            foreach (LifterPort lifterPort in this.lftSem.GetLifterPortAll().Values)
            {
                if (lifterPort.PortInOutMode == enumLifterPortDirection.OUT)
                {
                    destID.Add(lifterPort.HostID);
                }
            }
            this.cbDest.Items.AddRange(destID.ToArray());
        }
        /// <summary>
        /// 加载指令状态控件数据
        /// </summary>
        private void LoadcbCommandStatusData()
        {
            List<string> commandStatus = new List<string>();
            commandStatus.Add(enumTransferState.CREATE.ToString());
            commandStatus.Add(enumTransferState.LOADREQ.ToString());
            commandStatus.Add(enumTransferState.TRANSFERRING.ToString());
            commandStatus.Add(enumTransferState.ONSHUTTLE.ToString());
            commandStatus.Add(enumTransferState.COMPLETED.ToString());
            this.cbCommandStatus.Items.AddRange(commandStatus.ToArray());
        }

        private void cbCommandID_TextChanged(object sender, System.EventArgs e)
        {
            string commandID = this.cbCommandID.Text;
            TransferWorkItem transferWorkItem = this.lftSem.LDispatcher.GetJob(commandID);
            if (transferWorkItem != null)
            {
                this.cbMaterialID.Text = transferWorkItem.CarrierID;
                this.cbFrom.Text = transferWorkItem.Source;
                this.cbDest.Text = transferWorkItem.Dest;
                this.cbCommandStatus.Text = transferWorkItem.TransferState.ToString();
            }
        }
    }
}